package com.aiju.widgit.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.base.dialog.BaseDialog;
import com.aiju.common.PixValue;
import com.aiju.utils.EmojiFilter;
import com.aiju.utils.KeyBoardUtils;

/**
 * @author: 佐为
 * @date: 2016-11-17 15:55
 * @version: V1.0
 * @Description: 描述
 */
public class EditWeighDialog extends BaseDialog implements View.OnClickListener {

    private TextView titleTextView;
    private TextView leftTextView;
    private TextView rightTextView;
    private TextView msgTextView;

    public EditText getContentEditText() {
        return contentEditText;
    }

    private EditText contentEditText;

    public EditWeighDialog(Context context) {
        super(context);
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.dialog;
    }

    @Override
    protected View getView() {
        final View inflate = mLayoutInflater.inflate(R.layout.dialog_edit_weigh, null);
        titleTextView = (TextView) inflate.findViewById(R.id.tv_dialog_title);
        leftTextView = (TextView) inflate.findViewById(R.id.tv_dialog_left);
        rightTextView = (TextView) inflate.findViewById(R.id.tv_dialog_right);
        msgTextView = (TextView) inflate.findViewById(R.id.tv_dialog_msg);
        contentEditText = (EditText) inflate.findViewById(R.id.ev_dialog_content);
        leftTextView.setOnClickListener(this);
        rightTextView.setOnClickListener(this);
        KeyBoardUtils.openKeybord(contentEditText, mContext);
        setdismissListeren(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                InputMethodManager inputMgr = (InputMethodManager) mContext
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMgr.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
            }
        });

        contentEditText.setFilters(new InputFilter[]{
                new EmojiFilter()
        });
        return inflate;
    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public EditWeighDialog setLayoutParams() {
        WindowManager.LayoutParams lp = getDialogWindow().getAttributes();
        lp.width = PixValue.dp.valueOf(317);
        lp.height = PixValue.dp.valueOf(165);
        getDialogWindow().setGravity(Gravity.CENTER);
        getDialogWindow().setAttributes(lp);
        return this;
    }

    public EditWeighDialog setMsgText(String msg) {
        this.msgTextView.setText(msg);
        return this;
    }

    public EditWeighDialog setContentText(String content) {
        this.contentEditText.setText(content);
        this.contentEditText.setSelection(content.length());
        return this;
    }

    public EditWeighDialog setTitileText(String titile) {
        this.titleTextView.setText(titile);
        return this;
    }

    public EditWeighDialog setHit(String hit) {
        this.contentEditText.setHint(hit);
        return this;
    }

    public EditWeighDialog setInputType(int type) {
        this.contentEditText.setInputType(type);
        return this;
    }

    public EditWeighDialog setLeftText(String leftText) {
        this.leftTextView.setText(leftText);
        return this;
    }

    public EditWeighDialog setRightText(String rightText) {
        this.rightTextView.setText(rightText);
        return this;
    }

    private OnEditClick mOnEditClick;

    public EditWeighDialog setOnEditClick(OnEditClick onEditClick) {
        this.mOnEditClick = onEditClick;
        return this;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_dialog_left) {
            if (mOnEditClick != null) {
                mOnEditClick.onCancel();
                dismiss();
            }
        } else if (i == R.id.tv_dialog_right) {
            if (mOnEditClick != null) {
                mOnEditClick.onSubmit(contentEditText.getText().toString().trim());
                dismiss();
            }
        }
    }

    public interface OnEditClick {
        void onCancel();

        void onSubmit(String msg);
    }

}
